package com.tz.tzgw.pc.web;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import com.tz.tzgw.pc.web.annotation.AdminControllerAnnotation;
import com.tz.tzgw.pc.web.dao.*;
import com.tz.tzgw.pc.web.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@AdminControllerAnnotation
@RequestMapping("/api/")
public class ArticleController {
    private final ArticleDao articleDao;

    public ArticleController(ArticleDao articleDao) {
        this.articleDao = articleDao;
    }


    @RequestMapping("get_article_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_article_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "title", defaultValue = "") String title,@RequestParam(value = "columnid", defaultValue = "") Long column) {
        PageResult<Article> pageResult = articleDao.getArticlesByPageAndCondition(pageNum,20,title,column,"id");
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("article_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult article_act_do(@RequestBody Article article) {
        int suc = articleDao.insertOrUpdateArticle(article);
        if (suc  == 1) {
            return SaResult.data(article);
        } else {
            return SaResult.error("更新失败");
        }
    }

    @RequestMapping("get_article_detail")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_article_detail(@RequestParam("id") Long id) {
        Article article = articleDao.getArticleById(id);
        if (article != null) {
            return SaResult.data(article);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }


    /**
     * 后台banner删除
     * @param id
     * @return
     */
    @RequestMapping("article_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult article_del(@RequestParam("id") Integer id) {
        int suc = articleDao.deleteArticle(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }






}
